/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gray.swing.frame;

import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JPanel;
import gray.Frame;

/**
 *
 * @author martin
 */
public class SplitFrame extends JPanel implements Frame<Container>{
    private final Frame<Container> _left,
                                   _right;
    private boolean _orientation;
    
    private SplitFrame(final Frame<Container> left, final Frame<Container> right){
        super(new GridLayout(1,2));

        if (left instanceof SplitFrame)
            ((SplitFrame)left).flip();
        if (right instanceof SplitFrame)
            ((SplitFrame)right).flip();
        
        add(left.container());
        add(right.container());
        _orientation=true;
        
        _left = left;
        _right = right;
    }
    
    private void flip(){
        if (_orientation)
            setLayout(new GridLayout(2,1));
        else
            setLayout(new GridLayout(1,2));
        _orientation=!_orientation;

    }
    
    public static Frame<Container> factory(final Frame<Container> left, final Frame<Container> right){
        return new SplitFrame(left, right);
    }
    
    @Override
    public Container container() {
        return this;
    }

    @Override
    public Boolean ready() {
        return _left.ready() && _right.ready();
    }
}
